import 'dart:typed_data';

import 'package:flutter/material.dart';

///蓝牙操作
enum EMIBlueACT {
  none,

  ///蓝牙授权指令
  authentication,

  ///蓝牙启动车辆
  setDeviceOpen,

  ///蓝牙关闭车辆
  setDeviceClose,

  ///删除注册无感记录
  erssiIrkOpen,

  ///删除注册无感记录
  erssiIrkClose,

  ///开始一键寻车
  stareFoundDevice,

  ///结束一键寻车
  endFoundDevice,

  ///电池仓锁打开
  openTrunk,

  ///设置震动灵敏度
  vibrateSensitive,

  ///设置锁定状态
  lockStatusChange,

  ///自动锁车
  autoLock,

  ///蓝牙查询车辆状态
  queryOpenStatus,

  ///查询靠近解锁档位
  queryUnLockDistance,

  ///设置靠近解锁档位
  bleUnlockDistance,

  ///查询震动灵敏度
  queryVibrateSensitive,

  ///查询无感状态
  queryRssiIrk,

  ///查询电池电压电容
  queryElectrict,

  ///查询电池状态
  queryElectrictStatus,

  ///获取设备SN号 0-15位
  queryDeviceSN,

  ///获取设备唯一标识
  queryDeviceCharacteristic,

  ///获取设备名称和硬件版本
  queryDeviceNameAndVersion,

  ///获取中控固件信息
  queryDeviceOTAVersion,

  ///获取锁定状态
  queryLockStatus,

  ///查询信号强度
  query4GAndGPS,

  ///中控重启
  restart,
}

class EMIServiceUUID {
  static const serviceUUID = "F1CEA54F-449A-8717-0042-534332310A01";
  static const serviceWriteUUID = "F1CEA54F-449A-8717-0042-534332310B01";
  static const serviceRedUUID = "F1CEA54F-449A-8717-0042-534332310B02";
}

///蓝牙写入数据
class EMIBlueInstruction {
  static const title = 0xaa;

  ///设备登录校验
  static const loginReply = [title, 0x13, 0x00, 0x01, 0x02];

  ///设备登录校验
  static const loginVerify = [title, 0x03, 0x00, 0x01, 0x04];

  ///主动下发授权指令
  static const authentication = [title, 0x02, 0x00, 0x01];

  ///蓝牙启动关闭车辆
  static const setDeviceOpen = [title, 0x03, 0x01, 0x61, 0x01];

  ///蓝牙启动关闭车辆
  static const setDeviceClose = [title, 0x03, 0x01, 0x61, 0x00];

  ///删除注册无感记录
  static const erssiIrkOpen = [title, 0x03, 0xaa, 0x68, 0x00];

  ///删除注册无感记录
  static const erssiIrkClose = [title, 0x03, 0xaa, 0x68, 0x01];

  ///开始一键寻车
  static const stareFoundDevice = [title, 0x05, 0xaa, 0x63, 0x08, 0x08, 0x02];

  ///结束一键寻车
  static const endFoundDevice = [title, 0x05, 0xaa, 0x63, 0x08, 0x00, 0x02];

  ///电池仓锁打开
  static const openTrunk = [title, 0x04, 0x01, 0x62, 0x10, 0x10];

  ///设置震动灵敏度
  static const vibrateSensitive = [title, 0x05, 0x09, 0x65, 0x00, 0x00];

  ///蓝牙靠近解锁距离
  static const bleUnlockDistance = [
    title,
    0x08,
    0x08,
    0x66,
    0x08,
    0x00,
    0x00,
    0x00
  ];

  ///设置锁定状态
  static const lockStatusChange = [title, 0x03, 0x0a, 0x64];

  ///自动锁车
  static const autoLock = [
    title,
    0x08,
    0x0b,
    0x66,
    0x10,
    0x00,
    0x00,
    0x00,
    0x00
  ];

  ///蓝牙查询车辆状态
  static const queryOpenStatus = [title, 0x02, 0x01, 0x61];

  ///查询靠近解锁档位
  static const queryUnLockDistance = [title, 0x02, 0x30, 0x66];

  ///查询震动灵敏度
  static const queryVibrateSensitive = [title, 0x02, 0x31, 0x65];

  ///查询无感状态
  static const queryRssiIrk = [title, 0x02, 0x32, 0x68];

  ///查询电池电压电容
  static const queryElectrict = [title, 0x02, 0x33, 0x54];

  ///查询电池状态
  static const queryElectrictStatus = [title, 0x02, 0x34, 0x53];

  ///获取设备SN号 0-15位
  static const queryDeviceSN = [title, 0x02, 0x35, 0x11];

  ///获取设备唯一标识 0-7 UUId 8-13 mac地址 14-28IMEI号码 29-43IMSI号码 44-63 ICCID号码
  static const queryDeviceCharacteristic = [title, 0x02, 0x36, 0x12];

  ///获取设备名称和硬件版本
  static const queryDeviceNameAndVersion = [title, 0x02, 0x37, 0x13];

  ///获取中控固件信息
  static const queryDeviceOTAVersion = [title, 0x02, 0x38, 0x14];

  ///获取锁定状态
  static const queryLockStatus = [title, 0x02, 0x39, 0x64];

  ///查询信号强度
  static const query4GAndGPS = [title, 0x02, 0x3a, 0x74];

  ///中控重启
  static const restart = [title, 0x03, 0x01, 0x21, 0x00];
}

class CRC8 {
  static const int polynomial = 0x07;
  static const int initialValue = 0x00;

  static int calculateCRC(Uint8List data) {
    int crc = initialValue;

    for (int byte in data) {
      crc ^= byte; // crc ^= *data; data++;
      for (int i = 0; i < 8; i++) {
        if ((crc & 0x80) != 0) {
          crc = (crc << 1) ^ polynomial;
        } else {
          crc <<= 1;
        }
      }
      crc &= 0xFF; // 保持CRC为8位
    }

    return crc;
  }

  static bool didCheckCRC(List<int> datas) {
    int crc = datas.last;
    var dataed = datas.sublist(0, datas.length - 1);
    int crcrd = calculateCRC(Uint8List.fromList(dataed));
    debugPrint('CRC 校验 ---- $datas --$crcrd --$dataed');
    return crc == crcrd;
  }
}
